using Microsoft.Office.Interop.Excel;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationInvoice2
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private Range range;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationInvoice2(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeDataToExcel(string templetFile, DataSet dsTypeSubtotal, string sInvoiceCode, string sCompanyName, string sProjectName, string sInvoiceDate, string sLinkman, string sPhone, string sAddress, string sOperatorName)
		{
			this.OpenExcel(templetFile);
			this.MakeDataToExcel_1(dsTypeSubtotal, sInvoiceCode, sCompanyName, sProjectName, sInvoiceDate, sLinkman, sPhone, sAddress, sOperatorName);
		}

		private void MakeDataToExcel_1(DataSet dsTypeSubtotal, string sInvoiceCode, string sCompanyName, string sProjectName, string sInvoiceDate, string sLinkman, string sPhone, string sAddress, string sOperatorName)
		{
			try
			{
				int num = 6;
				this.workSheet = (_Worksheet)this.workBook.Worksheets[this.workBook.Worksheets.Count];
				this.workSheet.Activate();
				this.workSheet.Cells[2, 6]= sInvoiceCode;
				this.workSheet.Cells[3, 2]= sCompanyName + "(" + sProjectName + ")";
				this.workSheet.Cells[3, 6]= sInvoiceDate;
				this.workSheet.Cells[5, 2]= sAddress;
				decimal d = 0m;
				for (int i = 0; i < dsTypeSubtotal.Tables[0].Rows.Count; i++)
				{
					num++;
					int num2 = Convert.ToInt32(dsTypeSubtotal.Tables[0].Rows[i][1]);
					decimal d2 = Convert.ToDecimal(dsTypeSubtotal.Tables[0].Rows[i][2]);
					decimal d3 = Convert.ToDecimal(dsTypeSubtotal.Tables[0].Rows[i][4]);
					decimal d4 = Convert.ToDecimal((d2 * d3).ToString("F4"));
					d += d4;
					this.workSheet.Cells[num, 1]= dsTypeSubtotal.Tables[0].Rows[i][0].ToString();
					this.workSheet.Cells[num, 2]= "见附页";
					this.workSheet.Cells[num, 3]= num2.ToString();
					this.workSheet.Cells[num, 4]= d2.ToString("F2");
					this.workSheet.Cells[num, 5]= d3.ToString();
					this.workSheet.Cells[num, 6]= d4.ToString("F0");
				}
				for (int i = 0; i < dsTypeSubtotal.Tables[1].Rows.Count; i++)
				{
					num++;
					decimal num3 = Convert.ToDecimal(dsTypeSubtotal.Tables[1].Rows[i][1]);
					decimal num4 = Convert.ToDecimal(dsTypeSubtotal.Tables[1].Rows[i][2]);
					decimal d5 = Convert.ToDecimal((Convert.ToDecimal(dsTypeSubtotal.Tables[1].Rows[i][1]) * Convert.ToDecimal(dsTypeSubtotal.Tables[1].Rows[i][2])).ToString("F4"));
					d += d5;
					this.workSheet.Cells[num, 1]= dsTypeSubtotal.Tables[1].Rows[i][0].ToString();
					this.workSheet.Cells[num, 2]= "见附图";
					this.workSheet.Cells[num, 3]= num3.ToString();
					this.workSheet.Cells[num, 5]= num4.ToString();
					this.workSheet.Cells[num, 6]= d5.ToString();
				}
				this.range = this.workSheet.get_Range(this.workSheet.Cells[7, 7], this.workSheet.Cells[num, 7]);
				this.range.ClearContents();
				this.range.MergeCells=true;
				num++;
				this.workSheet.Cells[num, 1]= "小  计:";
				this.workSheet.Cells[num, 6]= d.ToString("F0");
				num++;
				this.range = this.workSheet.get_Range(this.workSheet.Cells[num, 1], this.workSheet.Cells[num, 7]);
				this.range.ClearContents();
				this.range.MergeCells=true;
				this.range.WrapText=true;
				this.range.HorizontalAlignment=-4131;
				this.range.Value2="金额(大写)：" + CommonMethod.MoneyToUpper(Convert.ToDecimal(d.ToString("F0")));
				this.range = this.workSheet.get_Range(this.workSheet.Cells[num + 1, 1], this.workSheet.Cells[num + 1, 7]);
				this.range.ClearContents();
				this.range.MergeCells=true;
				this.range.WrapText=true;
				this.range.RowHeight=18;
				this.range.Font.Size=(10);
				this.range.HorizontalAlignment=-4131;
				this.range.Value2="(此单共五联，白色统计联、红色客户联、蓝色保管联、绿色财务联、黄色存根联)";
				this.range = this.workSheet.get_Range(this.workSheet.Cells[num + 2, 1], this.workSheet.Cells[num + 2, 7]);
				this.range.ClearContents();
				this.range.MergeCells=true;
				this.range.WrapText=true;
				this.range.RowHeight=22;
				this.range.Font.Size=9;
				this.range.HorizontalAlignment=-4131;
				this.range.Value2="※ 1、提货时，客户按此单提货 2、在交货对产品外观、质量进行验收；玻璃为易碎品，如有破损问题，一经交付，本公司概不负责";
                if (num > 6)
                {
                    this.range = this.workSheet.get_Range(this.workSheet.Cells[7, 1], this.workSheet.Cells[num, 7]);
                    this.range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle=1;
                    this.range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = 1;
                }
				this.workSheet.Cells[num + 3, 1]= "客户签章:";
				this.workSheet.Cells[num + 3, 6]= "制单：";
				this.workSheet.Cells[num + 3, 7]= sOperatorName;
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
